# !/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import yaml
import logging

import logging.config


def read_logging_config(default_path: str = "logging.yml", env_key: str = "LOG_CFG") -> dict:
    path = default_path
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, "rt") as f:
            logging_config = yaml.safe_load(f.read())
        return logging_config
    elif os.path.exists(os.path.dirname(__file__) + '/' + path):
        path = os.path.dirname(__file__) + '/' + path
        with open(path, "rt") as f:
            logging_config = yaml.safe_load(f.read())
        return logging_config
    else:
        return {}


def setup_logging(logging_config: dict, default_level=logging.INFO):
    if logging_config:
        logging.config.dictConfig(logging_config)
    else:
        logging.basicConfig(level=default_level)


logconfig_dict = read_logging_config("logging.yml")
setup_logging(logconfig_dict)

logger = logging.getLogger("uvicorn")
